#include "server.h"

void init_server() {
	int create_socket, new_socket;
	socklen_t addrlen;
	char *buffer = malloc(BUF);
	ssize_t size;
	struct sockaddr_in address;
	const int y = 1;

	if ((create_socket = socket(AF_INET, SOCK_STREAM, 0)) > 0)
		printf("(SERVER) Socket wurde angelegt\n");

	setsockopt(create_socket, SOL_SOCKET, 
	SO_REUSEADDR, &y, sizeof(int));

	address.sin_family = AF_INET;
	address.sin_addr.s_addr = INADDR_ANY;
	address.sin_port = htons (15000);
	if (bind(create_socket, (struct sockaddr *) &address, sizeof (address))
			!= 0) {
		printf("(SERVER) Der Port ist nicht frei - belegt!\n");
	}
	listen(create_socket, 5);
	addrlen = sizeof(struct sockaddr_in);

	while (1) {
		new_socket = accept(create_socket, (struct sockaddr *) &address,
				&addrlen);
		if (new_socket > 0)
			printf("(SERVER) Ein Client (%s) ist verbunden ...\n",
					inet_ntoa(address.sin_addr));
		//do {
			//printf("Nachricht zum Versenden: ");
			//fgets(buffer, BUF, stdin);
			//send(new_socket, buffer, strlen(buffer), 0);
			size = recv(new_socket, buffer, BUF-1, 0);
			if (size > 0)
				buffer[size] = '\0';
			//gchar * user;
			//char *ip;
			//ip = inet_ntoa(address.sin_addr);
			gchar * msg = g_strconcat("--", buffer, "---", NULL); 
			printf("%s\n",msg);
			gdk_threads_enter();	
				//user = get_user_from_ip(ip);
				//printf("%s\n",ip);
				add_message(msg, "test");
			gdk_threads_leave();
			printf("(SERVER) Nachricht empfangen: %s\n", buffer);
		//} while (strcmp(buffer, "quit\n") != 0);
		close(new_socket);
	}
	close(create_socket);
}

